package auctionhaus



import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(ListingCreateService)
@Mock([Customer,Listing])
class ListingCreateServiceTests {

    Customer seller
    
    void setUp()
    {
        seller = new Customer(email: "pkleczka@gmail.com", password: "1234567")
    }
    
    void testCreateValidListing()
    {
        Date endDate = new Date() + 2
        
        def params = [name: "ServiceTestListing",
                      endDateTime: endDate,
                      startingBidPrice: 1.0f,
                      description: "Service test listing description.",
                      seller: seller]

        Listing l = service.createListing(params)

        assert !l.hasErrors()
        Listing foundL = Listing.findByName("ServiceTestListing")
        assert foundL.name == "ServiceTestListing"
        assert foundL.startingBidPrice == 1.0f
        assert foundL.endDateTime == endDate
        assert foundL.description == "Service test listing description."
        assert foundL.seller == seller
    }

    void testCreateValidListingNoDescription()
    {
        Date endDate = new Date() + 2

        def params = [name: "ServiceTestListing2",
                endDateTime: endDate,
                startingBidPrice: 1.0f,
                seller: seller]

        Listing l = service.createListing(params)

        assert !l.hasErrors()
        Listing foundL = Listing.findByName("ServiceTestListing2")
        assert foundL.name == "ServiceTestListing2"
        assert foundL.startingBidPrice == 1.0f
        assert foundL.endDateTime == endDate
        assert foundL.description == null
        assert foundL.seller == seller
    }

    void testCreateListingNoName()
    {
        Date endDate = new Date() + 2

        def params = [name: null,
                endDateTime: endDate,
                startingBidPrice: 1.0f,
                description: "Service test listing description.",
                seller: seller]

        Listing l = service.createListing(params)

        assert l.hasErrors()
        assert l.errors['name'].code  == "nullable"
    }

    void testCreateListingEndDateTimeInPast()
    {
        Date endDate = new Date() - 2

        def params = [name: "ServiceTestListing",
                endDateTime: endDate,
                startingBidPrice: 1.0f,
                description: "Service test listing description.",
                seller: seller]

        Listing l = service.createListing(params)

        assert l.hasErrors()
        assert l.errors['endDateTime'].code  == "validator.invalid"
    }

    void testCreateListingNegativeStartingBidPrice()
    {
        Date endDate = new Date() + 2

        def params = [name: "ServiceTestListing",
                endDateTime: endDate,
                startingBidPrice: -1.0f,
                description: "Service test listing description.",
                seller: seller]

        Listing l = service.createListing(params)

        assert l.hasErrors()
        assert l.errors['startingBidPrice'].code  == "min.notmet"
    }

    void testCreateListingNullSeller()
    {
        Date endDate = new Date() + 2

        def params = [name: "ServiceTestListing",
                endDateTime: endDate,
                startingBidPrice: 1.0f,
                description: "Service test listing description.",
                seller: null]

        Listing l = service.createListing(params)

        assert l.hasErrors()
        assert l.errors['seller'].code  == "nullable"
    }
}
